#include "i2c_controller.h"

#define AT24cxx_ADDR 0x50

int at24cxx_read(unsigned int addr,unsigned char* data,int len)
{
    i2c_msg_t msgs[2];
    //构造i2c msg
    msgs[0].addr =  AT24cxx_ADDR;
    msgs[0].flags = 0;//write addr	
    msgs[0].len = 1;
    msgs[0].buf = &addr;
    msgs[0].err = 0;
    msgs[0].cnt_transfered = -1;
    
    msgs[1].addr =  AT24cxx_ADDR;
    msgs[1].flags = 1;//read	
    msgs[1].len = len;
    msgs[1].buf = data;
    msgs[1].err = 0;
    msgs[1].cnt_transfered = -1;

    return i2c_transfer(&msgs,2);
}

int at24cxx_write(unsigned int addr,unsigned char* data,int len)
{
    i2c_msg_t msg;
    int i,err;
    unsigned char buf[2];
    for (i = 0; i < len; ++i) {

	buf[0] = addr++;
	buf[1] = data[i];

	//构造i2c msg
	msg.addr =  AT24cxx_ADDR;
	msg.flags = 0;//write	
	msg.len = 2;
	msg.buf = buf;
	msg.err = 0;
	msg.cnt_transfered = -1;
	//调用i2c transfer
	err = i2c_transfer(&msg,1);    
	if(err)
	    return err;
    }
    return 0;
}
